﻿using KZ.Express.ENT;
using KZ.Express.Resource;
using KZ.Utilities;
using System;
using System.Collections.Generic;
using System.Data;

namespace KZ.Express.Task
{
    public class CustomerGroupTask : TaskBase
    {
        private new PartnerGroupENT _entity;

        public CustomerGroupTask()
            : base()
        {
            base._entity = _entity = new PartnerGroupENT();
            _entitytype = _entity.GetType();
        }

        public void Save(List<DataRow> rows)
        {
            _dalBase.BeginTransaction();
            try
            {
                foreach (DataRow row in rows)
                {
                    string partnerGroupName = Utility.StringParse(row["PartnerGroupName"]);
                    if (!String.IsNullOrEmpty(partnerGroupName))
                    {
                        PartnerGroupENT partnerGroup = new PartnerGroupENT();
                        partnerGroup.PartnerGroupID = Utility.IntParse(row["PartnerGroupID"]);
                        partnerGroup.PartnerGroupName = partnerGroupName;

                        if (partnerGroup.PartnerGroupID == 0) base._dalBase.Insert(partnerGroup);
                        else base._dalBase.Update(partnerGroup);
                    }
                }
                _dalBase.CommitTransaction();
            }
            catch (DataAccessException ex)
            {
                base._dalBase.RollbackTransaction();
                throw new BusinessLogicException(ex.Message);
            }
            catch
            {
                base._dalBase.RollbackTransaction();
                throw new BusinessLogicException(Message.BLM_ERR_SAVE.ToString());
            }
        }
    }
}